package com.huluobo.a2109kotlinshopping.category

import android.content.Intent
import android.util.Log
import android.widget.Toast
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.ToastUtils
import com.huluobo.a2109kotlinshopping.R
import com.huluobo.a2109kotlinshopping.base.BaseMVVMFragment
import com.huluobo.a2109kotlinshopping.databinding.FragmentCategoryBinding
import com.huluobo.a2109kotlinshopping.detail.DetailActivity
import com.huluobo.a2109kotlinshopping.net.Const
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener

class CategoryFragment : BaseMVVMFragment<FragmentCategoryBinding>() {
    private lateinit var viewModel: CategoryViewModel

    //一级分类的adapter
    private lateinit var parentAdapter: CategoryParentAdapter

    //一级分类的数据源
    private val categoryParentList = mutableListOf<CategoryBean>()

    //二级分类的adapter
    private lateinit var childAdapter: CategoryChildAdapter

    //二级分类的数据源
    private val categoryChildList = mutableListOf<CategoryBean>()

    //三级分类的adapter
    private lateinit var goodsListAdapter: CategoryGoodsListAdapter

    //三级分类的数据源
    private val goodsList = mutableListOf<CategoryGoodsListBean>()

    //刷新时需要使用的page
    private var currentPage = 1

    //刷新时需要使用的三级列表id
    private var goodsListId = 0
    override fun getLayoutId(): Int = R.layout.fragment_category

    override fun initData() {
        viewModel = ViewModelProvider(requireActivity())[CategoryViewModel::class.java]

        viewModel.getCategory()

        //一二级列表的数据变化
        viewModel.success.observe(this) {
            if (it.code == 200) {
                //一级列表数据源初始化
                categoryParentList.clear()
                categoryParentList.addAll(it.data)
                parentAdapter.notifyDataSetChanged()

                //进入页面初始化二级列表数据源
                val data = categoryParentList[0].data
                if (data != null) {
                    categoryChildList.clear()
                    categoryChildList.addAll(data)
                    childAdapter.notifyDataSetChanged()

                    //初始化二级列表的同时,需要初始化三级列表
                    //需要拿到二级列表第0位的id
                    goodsListId = categoryChildList[0].id
                    viewModel.getCategoryGoodsList(goodsListId, currentPage, 10)
                }
            } else {
                ToastUtils.showLong(it.message)
            }
        }

        viewModel.failed.observe(this) {
            ToastUtils.showLong(it)
        }

        //三级列表的数据变化
        viewModel.getGoodsListSuccess.observe(this) {
            if (it.code == 200) {
                if (currentPage==1){
                    goodsList.clear()
                }
                goodsList.addAll(it.data)
                goodsListAdapter.notifyDataSetChanged()
            }
        }

        viewModel.getGoodsListFailed.observe(this) {
            Log.i("TagA", "获取三级列表失败:$it")
        }
    }

    override fun initView() {
        //初始化一级分类列表
        dataBinding.categoryRvParent.layoutManager = LinearLayoutManager(requireContext())
        parentAdapter = CategoryParentAdapter(categoryParentList)
        dataBinding.categoryRvParent.adapter = parentAdapter

        //一级列表的点击事件,切换二级的数据源
        parentAdapter.setOnItemClickListener { adapter, view, position ->
            parentAdapter.setSelectItemId(categoryParentList[position].id)
            val data = categoryParentList[position].data
            if (data != null) {
                categoryChildList.clear()
                categoryChildList.addAll(data)
                childAdapter.notifyDataSetChanged()

                //点击一级列表时,需要初始化三级列表
                if (categoryChildList.size != 0) {
                    goodsListId = categoryChildList[0].id
                    viewModel.getCategoryGoodsList(goodsListId, currentPage, 10)
                }
            }
        }

        //初始化二级分类列表
        dataBinding.categoryRvChild.layoutManager =
            GridLayoutManager(requireContext(), 2, RecyclerView.HORIZONTAL, false)
        childAdapter = CategoryChildAdapter(categoryChildList)
        dataBinding.categoryRvChild.adapter = childAdapter

        //二级列表的点击事件,切换三级列表
        childAdapter.setOnItemClickListener { adapter, view, position ->
            if (categoryChildList.size != 0) {
                goodsListId = categoryChildList[position].id
                viewModel.getCategoryGoodsList(categoryChildList[position].id, currentPage, 10)
            }
        }

        //初始化三级列表
        dataBinding.categoryRvGoodsList.layoutManager = LinearLayoutManager(requireContext())
        goodsListAdapter = CategoryGoodsListAdapter(goodsList)
        dataBinding.categoryRvGoodsList.adapter = goodsListAdapter

        //三级列表点击事件,跳转详情
        goodsListAdapter.setOnItemClickListener { adapter, view, position ->
            val intent = Intent(requireActivity(), DetailActivity::class.java)
            intent.putExtra(Const.PARAM_GOODS_ID, goodsList[position].id)
            startActivity(intent)
        }

        //初始化刷新框架
        dataBinding.categorySl.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                if (refreshLayout.isRefreshing) {
                    refreshLayout.finishRefresh()
                }
                currentPage = 1
                viewModel.getCategoryGoodsList(goodsListId, currentPage, 10)
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                if (refreshLayout.isLoading) {
                    refreshLayout.finishLoadMore()
                }
                currentPage += 1
                viewModel.getCategoryGoodsList(goodsListId, currentPage, 10)
            }
        })
    }

}